package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.ShoppingCartGoodsModel;
import com.zw.yzk.kabu.model.ShoppingCartModel;
import com.zw.yzk.kabu.utils.CalculateUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhanwei on 2017/2/7.
 */

public class ShoppingCartListAdapter extends RecyclerView.Adapter<ShoppingHolder> implements ShoppingHolder.OnShopStateChangedListener {

    private List<ShoppingCartModel> dataList;
    private Context context;
    private boolean editMode;

    public ShoppingCartListAdapter(List<ShoppingCartModel> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void refresh(List<ShoppingCartModel> list) {
        dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public List<ShoppingCartModel> getShoppingCartList() {
        return dataList;
    }

    public void clear() {
        dataList.clear();
        if (listener != null) {
            listener.onCoastChanged(0f);
        }
        notifyDataSetChanged();
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void changeMode(boolean mode) {
        editMode = mode;
        notifyDataSetChanged();
    }

    public void selectAll() {
        for (ShoppingCartModel model : dataList) {
            model.setSelected(true);
        }
        notifyDataSetChanged();
    }

    public void unSelectAll() {
        for (ShoppingCartModel model : dataList) {
            model.setSelected(false);
        }
        notifyDataSetChanged();
    }

    //计算选中店铺的选中商品的总额
    private float calculateSelectedTotalPrice() {
        BigDecimal result = new BigDecimal(0f);
        for (ShoppingCartModel model : dataList) {
            BigDecimal price = new BigDecimal(model.getSelectedCoast());
            result = result.add(price);
        }
        //四舍五入
        return CalculateUtils.setScale(result, 2);
    }

    //计算总金额
    public float calculatTotalPrice() {
        BigDecimal result = new BigDecimal(0f);
        for (ShoppingCartModel model : dataList) {
            BigDecimal price = new BigDecimal(0);
            for(ShoppingCartGoodsModel goods : model.getModels()) {
                BigDecimal goodsPrice = new BigDecimal(goods.getPrice());
                BigDecimal count = new BigDecimal(goods.getCount());

                price = price.add(goodsPrice.multiply(count));
            }
            result = result.add(price);
        }
        //四舍五入
        return CalculateUtils.setScale(result, 2);
    }

    @Override
    public ShoppingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_shopping_cart_list, parent, false);
        ShoppingHolder holder = new ShoppingHolder(view);
        holder.setOnShopCoastChangedListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ShoppingHolder holder, int position) {
        ShoppingCartModel model = dataList.get(position);
        holder.setAdapter(context, model.getModels(), editMode);
        holder.shop.setText(model.getShopName());
        holder.select.setChecked(model.isSelected());
        if (model.isEditMode()) {
            holder.edit.setVisibility(View.GONE);
            holder.complete.setVisibility(View.VISIBLE);
        } else {
            holder.edit.setVisibility(View.VISIBLE);
            holder.complete.setVisibility(View.GONE);
        }
        if (model.isSelected()) {
            holder.selectAll();
        } else {
            holder.unSelectAll();
        }

        holder.edit.setVisibility(View.GONE);
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                dataList.get(position).setEditMode(true);

                holder.edit.setVisibility(View.GONE);
                holder.complete.setVisibility(View.VISIBLE);
                holder.changeMode(true);
            }
        });
        holder.complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                dataList.get(position).setEditMode(false);

                holder.edit.setVisibility(View.VISIBLE);
                holder.complete.setVisibility(View.GONE);
                holder.changeMode(false);
                if (listener != null) {
                    listener.onEditShop(dataList.get(position).getId());
                }
            }
        });
        holder.selectLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                dataList.get(position).setSelected(!dataList.get(position).isSelected());

                if (dataList.get(position).isSelected()) {
                    holder.selectAll();
                    holder.select.setChecked(true);
                } else {
                    holder.unSelectAll();
                    holder.select.setChecked(false);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public void onStateChanged(float coast, int shopId) {
        for (ShoppingCartModel model : dataList) {
            if (model.getId() == shopId) {
                model.setSelectedCoast(coast);
            }
        }
        if (listener != null) {
            listener.onCoastChanged(calculateSelectedTotalPrice());
        }
    }

    @Override
    public void editGoods(ShoppingCartGoodsModel model) {
        if (listener != null) {
            listener.onEditGoods(model);
        }
    }

    public interface OnCoastChangedListener {
        void onCoastChanged(float price);

        void onEditGoods(ShoppingCartGoodsModel model);

        void onEditShop(int shopId);
    }

    private OnCoastChangedListener listener;

    public void setOnTotalPriceChangedListener(OnCoastChangedListener listener) {
        this.listener = listener;
    }
}
